package com.svms.service.dml.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.svms.domain.dao.SvmsSpareStaticMapper;
import com.svms.model.SvmsSpareStatic;
import com.svms.service.dml.iface.SpareDetailsDMLIF;
import com.svms.service.factory.SvmsServiceFactory;
import com.svms.service.mapper.SpareCodeMapper;
import com.svms.service.util.NumberGeneratorUtils;
import com.svms.service.vo.MovementVO;
import com.svms.service.vo.SpareCodesVO;

public class SpareDetailsDML implements SpareDetailsDMLIF {

	@Autowired
	private SvmsSpareStaticMapper svmsSpareStaticMapper;

	@Override
	public SpareCodesVO insertNewSpareDetail(SpareCodesVO spareStatic) {
		try {

			spareStatic.setSpareId(String.valueOf(NumberGeneratorUtils
					.generateSpareCodeId()));

			svmsSpareStaticMapper.insert(SpareCodeMapper
					.serviceToDml(spareStatic));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return spareStatic;

	}

	@Override
	public SpareCodesVO deleteSpareDetail(SpareCodesVO spareStatic) {

		try {
			svmsSpareStaticMapper.deleteByPrimaryKey(Long.parseLong(spareStatic
					.getSpareId()));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return spareStatic;
	}

	@Override
	public SpareCodesVO getSpareDetails(SpareCodesVO spareStatic) {
		SpareCodesVO spareCodesVO = spareStatic;
		try {
			spareCodesVO = SpareCodeMapper
					.dmlToService(svmsSpareStaticMapper.selectByPrimaryKey(Long
							.parseLong(spareStatic.getSpareId())));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spareCodesVO;
	}

	@Override
	public List<SpareCodesVO> getAllSparesDetailsList() {
		List<SpareCodesVO> sparesList = new ArrayList<SpareCodesVO>();

		try {
			List<SvmsSpareStatic> spareStatic = svmsSpareStaticMapper
					.getEntireTableData();
			for (SvmsSpareStatic svmsSpareStatic : spareStatic) {
				sparesList.add(SpareCodeMapper.dmlToService(svmsSpareStatic));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sparesList;
	}

	@Override
	public SpareCodesVO editSpareDetail(SpareCodesVO spareCodesVO) {
		try {

			svmsSpareStaticMapper.updateByPrimaryKey(SpareCodeMapper
					.serviceToDml(spareCodesVO));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return spareCodesVO;
	}

}
